Databases are used to store information for an innumerable number of applications, including various commercial, industrial, technical, scientific and educational applications. As the reliance on information increases, both the volume of information stored in most databases, as well as the number of users wishing to access that information, likewise increases. As the volume of information in a database, and the number of users wishing to access the database, increases, the amount of computing resources required to manage such a database increases as well.
Database management systems (DBMS's), which are the computer programs that are used to access the information stored in databases, therefore often require tremendous resources to handle the heavy workloads placed on such systems. As such, significant resources have been devoted to increasing the performance of database management systems with respect to processing searches, or queries, to databases.
Improvements to both computer hardware and software have improved the capacities of conventional database management systems. For example, in the hardware realm, increases in microprocessor performance, coupled with improved memory management systems, have improved the number of queries that a particular microprocessor can perform in a given unit of time. Furthermore, the use of multiple microprocessors and/or multiple networked computers has further increased the capacities of many database management systems.
From a software standpoint, the use of relational databases, which organize information into formally-defined tables, and which are typically accessed using a standardized language such as Structured Query Language (SQL), has substantially improved processing efficiency, as well as substantially simplified the creation, organization, and extension of information within a database. Furthermore, significant development efforts have been directed toward query “optimization”, whereby the execution of particular searches, or queries, is optimized in an automated manner to minimize the amount of resources required to execute each query.
Through the incorporation of various hardware and software improvements, many high performance database management systems are able to handle hundreds or even thousands of queries each second, even on databases containing millions or billions of records. However, further increases in information volume and workload are inevitable, so continued advancements in database management systems are still required.
Many conventional database management systems, for example, are inherently interpretive systems, where queries are written in an interpretive language such as SQL, and dynamically interpreted by a query engine during query execution. Runtime interpretation in a computer environment, however, almost always results in reduced performance as compared to direct execution of executable code. Other conventional database management systems have attempted to reduce the amount of interpretation required to execute a query, typically by generating queries that comprise assembled blocks of code, such that a query engine needs only to select and execute code blocks that correspond to particular query instructions.
In addition, many conventional database management systems incorporate query optimizers, which operate to optimize the performance of a query to be executed by a query engine. Such query optimizers often operate by selecting from among multiple “plans”, or possible implementations of a query, so as to execute the query with the greatest efficiency.
As an example, in a relational database, data is stored in tables, where the rows, or entries, in the tables generally correspond to data records, and the columns generally correspond to the fields in each data record. Thus, for example, in a table, “empinf,” that stores information about a company's employees, the table may include columns, or fields, representing first name, last name, location, salary, department, job identifier, etc., with each row representing each record in the table.
To perform a search of a table to locate records that match a particular criterion, a table can often be analyzed using either table scans or index probes. A table scan operates more or less by sequentially stepping through each record in a table to find matching records, while an index probe is keyed off of an index that is generated for the table. A table scan is typically more efficient when a large number of records match the criterion, while an index probe (which has additional overhead associated with generating the index) is typically more efficient when only a small number of records match the criterion.
Thus, using the above example, assuming a query was directed to finding all employees that had a salary below $50,000, assuming that most employees had a salary below that range, a table scan would typically be more efficient than an index probe. On the other hand, assuming a query was directed to finding all employees having a first name of “David”, an index probe would typically be more efficient, as the overhead associated with indexing the records based upon first name would be offset by the ability to directly lookup the relatively few number of records that matched the criterion.
Query optimizers typically rely on statistics, developed over time, to select among multiple plans so that the most efficient plan for a particular type of query is selected. Therefore, a query optimizer in the aforementioned example might recognize that a query directed to the salary field typically generates a large number of matching records, and as such, a table scan-based plan would be the most appropriate for queries directed to the salary field. Likewise, such a query optimizer might recognize that a query directed to the first name field typically generates a small number of matching records, and as such, an index probe-based plan would be the most appropriate for queries directed to the first name field.
But even with using statistics and other optimization techniques, some query operations may still result in large overhead requirements. For example, SQL defines clauses such as “rollup”, “cube”, and “grouping sets” as shorthand notations for the union of multiple grouping/aggregations queries. Referring again to the example above, the following query:                SELECT jobid, dept, max(salary) FROM empinf GROUP BY GROUPING SETS(jobid,dept)implicitly produces a “union all” of two disjoint result sets, one grouped by jobid, and the other by dept, from table empinf in the database.        
An optimizer, when evaluating this query would likely rewrite the query similar to the following:                SELECT null( ),dept,max(salary) FROM empinf GROUP BY dept UNION ALL SELECT jobid,null( ),max(salary) FROM empinf GROUP BY jobid.        
As can be seen from the above rewrite, the grouping/aggregation occurs sequentially, and the table empinf is accessed two times. Since the query text can be arbitrarily complex, with joins, and many tables, and the number of distinct grouping/aggregation values specified can be quite large, the overhead incurred by repeatedly executing the sub-query (in this case the scan of table empinf) can be large both in terms of cycles used as well as excessive I/O requirements. Even if the sub-query is materialized once, the temporary result from the scan of table empinf is still scanned twice to generate each distinct grouping/aggregation.
Therefore, there is a need in the art to be able to produce desired result sets from arbitrarily complex common sub-queries, such as above, with only a single pass through the dataspace, and without the need to store temporary, intermediate results.